package org.bigmarketapi.gatewaycenter.domain.registry.service;

import com.alibaba.fastjson.JSON;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.bigmarketapi.gatewaycenter.domain.registry.model.aggre.appRegistryAggregate;
import org.bigmarketapi.gatewaycenter.domain.registry.model.aggre.interfaceAggregate;
import org.bigmarketapi.gatewaycenter.domain.registry.model.vo.AppInterfaceMethodVO;
import org.bigmarketapi.gatewaycenter.domain.registry.model.vo.AppInterfaceVO;
import org.bigmarketapi.gatewaycenter.domain.registry.model.vo.AppSystemVO;
import org.bigmarketapi.gatewaycenter.domain.registry.repository.registryRepository;
import org.bigmarketapi.gatewaycenter.infrastructure.persistent.po.AppInterfaceMethod;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @BelongsProject: Gateway-Center
 * @BelongsPackage: org.bigmarketapi.gatewaycenter.domain.registry.service
 * @Author: dujiaqi
 * @CreateTime: 2024-11-30  22:26
 * @Description: TODO
 * @Version: 1.0
 */
@Service
@Slf4j
public class IRegistryImpl implements IRegistry {
    @Resource
    registryRepository repository;

    @Override
    public void registerApplication(AppSystemVO applicationSystemVO) {
        repository.insertApplication(applicationSystemVO);
    }

    @Override
    public void registerApplicationInterface(AppInterfaceVO applicationInterfaceVO) {
        repository.insertApplicationInterface(applicationInterfaceVO);
    }

    @Override
    public void registerApplicationInterfaceMethod(AppInterfaceMethodVO applicationInterfaceMethodVO) {
        repository.insertApplicationInterfaceMethod(applicationInterfaceMethodVO);
    }

    @Override
    public appRegistryAggregate queryAppRegistry(String gatewayId) {
         String systemId=repository.querySystemIdByGatewayId(gatewayId);
        //todo 提供一个应用聚合： 应用系统（1:n（系统接口（1: n(实现接口的方法 )))
        AppSystemVO appSystemVO = repository.queryAppSystemBySystId(systemId);
        List<AppInterfaceVO> interfaceList = repository.queryAppInterfaceBySystemId(appSystemVO.getSystemId());
        final List<String> interfaceIds = interfaceList.stream().map(AppInterfaceVO::getInterfaceId).toList();
        List<AppInterfaceMethodVO> methods = repository.queryInterMethods(interfaceIds);
        // 过滤掉 interfaceId 为 null 的 AppInterfaceMethodVO 对象
        List<AppInterfaceMethodVO> filteredMethods = methods.stream()
                .filter(method -> method.getInterfaceId() != null)
                .toList();
        final Map<String, List<AppInterfaceMethodVO>> interMethods = filteredMethods.stream().collect(Collectors.groupingBy(AppInterfaceMethodVO::getInterfaceId));
        List<interfaceAggregate> aggList = new ArrayList<>();
        for (AppInterfaceVO id : interfaceList) {
            final List<AppInterfaceMethodVO> methodVOList = interMethods.get(id.getInterfaceId());
            final   interfaceAggregate interAgg = interfaceAggregate.builder()
                    .interfaceId(appSystemVO.getSystemId())
                    .interfaceName(appSystemVO.getSystemName())
                    .interfaceVersion(id.getInterfaceVersion())
                    .methodList(methodVOList)
                    .systemId(appSystemVO.getSystemId()).build();
                    aggList.add(interAgg);
        }
        return  appRegistryAggregate.builder()
                .systemId(appSystemVO.getSystemId())
                .systemName(appSystemVO.getSystemName())
                .systemType(appSystemVO.getSystemType())
                .systemRegistry(appSystemVO.getSystemRegistry())
                .interfaceList(aggList)
                .build();

    }
}
